﻿using System;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;

namespace YJKArch.ToolsModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdHYTestRoom : YJKCommandAuto
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
			Result result;
			try
			{
				Level currentViewLevel = AssistFunc.GetCurrentViewLevel(document);
				Phase currentViewPhase = AssistFunc.GetCurrentViewPhase(document);
				PlanTopology planTopology = document.get_PlanTopology(currentViewLevel);
				double num = AssistFunc.mmToFeet(10.0);
				num *= num;
				int num2 = 101;
				foreach (object obj in planTopology.Circuits)
				{
					PlanCircuit planCircuit = (PlanCircuit)obj;
					if (planCircuit != null && !this.LessThan(planCircuit.Area, num))
					{
						Room room = document.Create.NewRoom(currentViewPhase);
						string name = "Room " + num2.ToString();
						room.Name = name;
						room.Number = Convert.ToString(num2);
						PlanCircuit planCircuit2 = planCircuit;
						document.Create.NewRoom(room, planCircuit2);
						num2++;
					}
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private const double _epsDouble = 1E-09;
	}
}
